import os
import shutil
import subprocess

def build_app():
    # 确保目录存在
    directories = ['static', 'templates', 'static/uploads']
    for directory in directories:
        if not os.path.exists(directory):
            os.makedirs(directory)

    # 使用 PyInstaller 命令行打包
    cmd = [
        'pyinstaller',
        '--name=EquipmentManager',
        '--onefile',
        '--windowed',
        '--add-data=templates;templates',
        '--add-data=static;static',
        'run.py'
    ]
    
    subprocess.run(cmd)

    # 创建发布目录
    if not os.path.exists('release'):
        os.makedirs('release')

    # 复制必要文件到发布目录
    files_to_copy = [
        ('equipment.db', 'equipment.db'),
        ('README.txt', 'README.txt'),
    ]

    for src, dst in files_to_copy:
        if os.path.exists(src):
            shutil.copy2(src, os.path.join('release', dst))

    # 复制生成的exe到发布目录
    shutil.copy2(
        os.path.join('dist', 'EquipmentManager.exe'),
        os.path.join('release', 'EquipmentManager.exe')
    )

    print("打包完成！")
    print("发布文件已生成在: release 目录")

if __name__ == '__main__':
    build_app() 